package find.chat.test;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.os.Handler;
import android.test.suitebuilder.annotation.SmallTest;
import find.chat.*;
import find.chat.R;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.test.TouchUtils;
import android.test.ActivityInstrumentationTestCase2;

public class friendlist extends ActivityInstrumentationTestCase2<FriendList> {
	private FriendList mActivity;
	ListView friend_list;
	View viewToClick;

	public friendlist() {
		super(FriendList.class);
	}

	protected void setUp() throws Exception {
		// this method is called every time before any test execution
		super.setUp();

		mActivity = (FriendList) getActivity(); // get current activity
		friend_list = (ListView) mActivity.getListView();
		viewToClick = friend_list.getChildAt(0);
		Thread loginThread = new Thread(){
			@Override
			public void run() {
				String result = null;
				try {
					result =  mActivity.imService.authenticateUser("chung","12345");
				} catch (UnsupportedEncodingException e) {
					
					e.printStackTrace();
				}
					
				}			
		};
		loginThread.start();
		// link the objects with the activity objects
	}

	@SmallTest
	// SmallTest: this test doesn't interact with any file system or network.
	public void testView() { // checks if the activity is created
		assertNotNull(getActivity());
	}
	public void testingaddfriend(){
	ActivityMonitor am = getInstrumentation().addMonitor(AddFriend.class.getName(), null, false);

	// Click the menu option
	getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
	getInstrumentation().invokeMenuActionSync(mActivity, Menu.FIRST, 0);

	Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
	assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
	a.finish();
	}
/*	public void testingcancel(){
		getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
		getInstrumentation().invokeMenuActionSync(mActivity, 2, 0);
	}*/
	
	public void testingShortClick(){
		ActivityMonitor am = getInstrumentation().addMonitor(Messaging.class.getName(), null, false);
		TouchUtils.clickView(this, viewToClick);
		Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
		assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
		a.finish();
	}
	public void testingLongClickYes(){
		TouchUtils.longClickView(this, viewToClick);
		assertTrue(mActivity.alertDialog.isShowing());
		Button buttonToClick = mActivity.alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
		TouchUtils.clickView(this, buttonToClick);
		assertTrue(buttonToClick.performClick());
	}
	public void testingLongClickNo(){
		TouchUtils.longClickView(this, viewToClick);
		assertTrue(mActivity.alertDialog.isShowing());
		Button buttonToClick = mActivity.alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
		TouchUtils.clickView(this, buttonToClick);
		assertTrue(buttonToClick.performClick());
	}
}
